package com.hermit.qmst.tools;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import java.util.HashMap;

/**
 * Created by hermithermit on 15/2/16.
 */
public class Preferences {

    public static final String MY_PHONE = "my_phone";
    public static final String USER_ID="user_id";
    public static final String MY_PASSWORD = "my_password";
    public static final String AUTO_RING = "auto_ring";
    public static final String CALL_WAIT_RING = "call_wait_ring";//拨号等待音
    public static final String DIAL_KEY_TONE = "dial_key_tone";//按键音
    public static final String DIAL_KEY_VIBRATE = "dial_key_vibrate";//按键震动
    public static final String SERVICE_CALL = "service_call";//客服电话
    public static final String WEBSITE = "web_site";
    public static final String SIP_ACCOUNT = "sip_account";
    public static final String SIP_AGENT = "sip_agent";
    public static final String SIP_AGENT_THIRD = "sip_agent_third";  //三级代理商
    public static final String CALL_WAY = "call_way";

    private SharedPreferences mPrefs;
    private SharedPreferences.Editor mEditor;
    private Context mContext;

    public final static HashMap<String, String> STRING_PREFS = new HashMap<String, String>() {
        private static final long serialVersionUID = 1L;

        {
            put(CALL_WAY, "0");
        }
    };

    public final static HashMap<String, Boolean> BOOLEAN_PREFS = new HashMap<String, Boolean>() {
        private static final long serialVersionUID = 1L;

        {
            put(AUTO_RING, true);
            put(CALL_WAIT_RING, true);
            put(DIAL_KEY_TONE, false);
            put(DIAL_KEY_VIBRATE, false);
        }
    };

    public final static HashMap<String, Float> FLOAT_PREFS = new HashMap<String, Float>() {
        private static final long serialVersionUID = 1L;

        {
        }
    };

    public final static HashMap<String, Integer> INTEGER_PREFS = new HashMap<String, Integer>() {
        private static final long serialVersionUID = 1L;

        {

        }
    };


    public Preferences(Context pContext) {
        mContext = pContext;
        mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
    }

    public void startEditor() {
        mEditor = mPrefs.edit();

    }

    public void endEditor() {
        if (mEditor != null) {
            mEditor.commit();
            mEditor = null;
        }
    }

    public void setPreferenceStringValue(String key, String value) {
        if (mEditor == null) {
            SharedPreferences.Editor editor = mPrefs.edit();
            editor.putString(key, value);
            editor.commit();
        } else {
            mEditor.putString(key, value);
        }
    }

    public void setPreferenceBooleanValue(String key, boolean value) {
        if (mEditor == null) {
            SharedPreferences.Editor editor = mPrefs.edit();
            editor.putBoolean(key, value);
            editor.commit();
        } else {
            mEditor.putBoolean(key, value);
        }
    }

    public void setPreferenceFloatValue(String key, float value) {
        if (mEditor == null) {
            SharedPreferences.Editor editor = mPrefs.edit();
            editor.putFloat(key, value);
            editor.commit();
        } else {
            mEditor.putFloat(key, value);
        }
    }

    public void setPreferenceIntValue(String key, int value) {
        if (mEditor == null) {
            SharedPreferences.Editor editor = mPrefs.edit();
            editor.putInt(key, value);
            editor.commit();
        } else {
            mEditor.putFloat(key, value);
        }
    }


    // For boolean
    private static Boolean gPrefBooleanValue(SharedPreferences aPrefs, String key) {
        if (aPrefs == null) {
            return BOOLEAN_PREFS.get(key);
        }
        if (BOOLEAN_PREFS.containsKey(key)) {
            return aPrefs.getBoolean(key, BOOLEAN_PREFS.get(key));
        }
        if (aPrefs.contains(key)) {
            return aPrefs.getBoolean(key, false);
        }
        return null;
    }

    //Private static getters
    // For string
    private static String gPrefStringValue(SharedPreferences aPrefs, String key) {
        if (aPrefs == null) {
            return STRING_PREFS.get(key);
        }
        if (STRING_PREFS.containsKey(key)) {
            return aPrefs.getString(key, STRING_PREFS.get(key));
        }
        return aPrefs.getString(key, (String) null);
    }

    private static Float gPrefFloatValue(SharedPreferences aPrefs, String key) {
        if (aPrefs == null) {
            return FLOAT_PREFS.get(key);
        }
        if (FLOAT_PREFS.containsKey(key)) {
            return aPrefs.getFloat(key, FLOAT_PREFS.get(key));
        }
        if (aPrefs.contains(key)) {
            return aPrefs.getFloat(key, 0.0f);
        }
        return null;
    }

    public static Class<?> gPrefClass(String key) {
        if (STRING_PREFS.containsKey(key)) {
            return String.class;
        } else if (BOOLEAN_PREFS.containsKey(key)) {
            return Boolean.class;
        } else if (FLOAT_PREFS.containsKey(key)) {
            return Float.class;
        }
        return null;
    }


    /**
     * Get string preference value
     *
     * @param key the key preference to retrieve
     * @return the value
     */
    public String getPreferenceStringValue(String key) {
        return gPrefStringValue(mPrefs, key);
    }

    /**
     * Get boolean preference value
     *
     * @param key the key preference to retrieve
     * @return the value
     */
    public Boolean getPreferenceBooleanValue(String key) {
        return gPrefBooleanValue(mPrefs, key);
    }

    /**
     * Get float preference value
     *
     * @param key the key preference to retrieve
     * @return the value
     */
    public Float getPreferenceFloatValue(String key) {
        return gPrefFloatValue(mPrefs, key);
    }

    /**
     * Get integer preference value
     *
     * @param key the key preference to retrieve
     * @return the value
     */
    public Integer getPreferenceIntegerValue(String key) {
        try {
            return Integer.parseInt(getPreferenceStringValue(key));
        } catch (NumberFormatException e) {
        }
        String val = STRING_PREFS.get(key);
        if (val != null) {
            return Integer.parseInt(val);
        }
        return null;
    }

    /**
     * Set all values to default
     */
    public void resetAllDefaultValues() {
        for (String key : STRING_PREFS.keySet()) {
            setPreferenceStringValue(key, STRING_PREFS.get(key));
        }
        for (String key : BOOLEAN_PREFS.keySet()) {
            setPreferenceBooleanValue(key, BOOLEAN_PREFS.get(key));
        }
        for (String key : FLOAT_PREFS.keySet()) {
            setPreferenceFloatValue(key, FLOAT_PREFS.get(key));
        }
    }


}
